package com.bytz.modules.cms.domain.impl.order;

import cn.hutool.core.util.BooleanUtil;
import com.bytz.common.exception.BytzBootException;
import com.bytz.modules.cms.domain.MallOrderProcessor;
import com.bytz.modules.cms.domain.model.order.MallOrderProcessContext;
import com.bytz.modules.cms.domain.util.PriceComputerUtil;
import com.bytz.modules.cms.order.model.OrderItemModel;
import com.bytz.modules.cms.order.model.OrderModel;
import com.bytz.modules.cms.reseller.entity.CouponReseller;
import com.bytz.modules.cms.reseller.service.ICouponResellerService;
import org.springframework.beans.factory.annotation.Autowired;

import java.math.BigDecimal;
import java.util.List;

/**
 * 订单优惠券处理器 5
 * @author shanxuezhong
 */
public class MallOrderCouponProcessor extends MallOrderAbstractProcessor implements MallOrderProcessor {


    private ICouponResellerService couponService;
    public MallOrderCouponProcessor(int sequence) {
        super(sequence);
    }
    @Autowired
    public void setCouponService(ICouponResellerService couponService) {
        this.couponService = couponService;
    }

    @Override
    public void doProcess(MallOrderProcessContext context) {
        OrderModel order = context.getOrder();
        String couponId = order.getCouponId();

        if (BooleanUtil.isTrue(context.getStandardOrder()) && couponId != null) {
            // 从db查找优惠券
            CouponReseller coupon = couponService.getValidCouponById(couponId, context.getResellerId());
            if (coupon == null) {
                throw new BytzBootException("优惠券不可使用");
            }

            BigDecimal orderTotalPrice = order.getTotalPrice();

            // 核对优惠券
            if (orderTotalPrice.compareTo(coupon.getMinPrice()) < 0) {
                throw new BytzBootException("没有达到优惠券使用条件，无法保存订单信息");
            }


            // 核对商品总优惠价
            BigDecimal discountPrice = coupon.getDiscountPrice();
            BigDecimal totalDiscount = BigDecimal.ZERO;

            List<OrderItemModel> orderItemList = order.getOrderItemList();

            for (int i = 0, lastIndex = orderItemList.size() - 1; i <= lastIndex; i++) {
                OrderItemModel item = orderItemList.get(i);
                BigDecimal deduction;

                if (i != lastIndex) {
                    // 分派比例 = 这一行的总钱 / 订单的总钱
                    BigDecimal ratio = PriceComputerUtil.divide(item.getTotalTaxPrice(), orderTotalPrice);
                    deduction = PriceComputerUtil.scale(discountPrice.multiply(ratio));
                } else {
                    // 最后一行，直接剩下多少，分多少
                    deduction = PriceComputerUtil.scale(discountPrice.subtract(totalDiscount));
                }

                totalDiscount = totalDiscount.add(deduction);

                // 设置优惠价
                item.setDeductionPrice(deduction);

                // 重新计算税后总价
                item.setTotalTaxPrice(item.getTotalTaxPrice().subtract(deduction));
            }

            // 设置订单的优惠价
            order.setDeductionPrice(totalDiscount);
            // 重新计算总价
            order.setTotalPrice(order.getTotalPrice().subtract(totalDiscount));
        }
    }
}